package com.gongpingjialoan.activity.car;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseScrollFragment;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.adapter.car.FindCarAdapter;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.RecordBean;
import com.gongpingjialoan.data.FindCarData;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.PreferenceUtils;
import com.handmark.pulltorefresh.library.swipemenu.library.SwipeMenu;
import com.handmark.pulltorefresh.library.swipemenu.library.SwipeMenuCreator;
import com.handmark.pulltorefresh.library.swipemenu.library.SwipeMenuItem;
import com.handmark.pulltorefresh.library.swipemenu.library.SwipeMenuListView;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/6/22.
 */
public class FindCarFragment extends BaseScrollFragment implements View.OnClickListener{

    private  View view;

    private SwipeMenuListView mSwipeMenuListView;

    private FindCarAdapter mFindCarAdapter;

    private PushSettingActivity mPushSettingActivity;

    private Button mButton;

    private NetDataJson deleteNetDataJson;

    private RecordBean.DataEntity.HelpFindCarEntity mHelpFindCarEntity;

    private List<RecordBean.DataEntity.HelpFindCarEntity> mRecordBeans;

    private PreferenceUtils mPreferenceUtils;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        action = "my_subscribeRecord_findCar";
        isScrollPage = true;
        view = inflater.inflate(R.layout.remind_lauout , container , false);
        mSwipeMenuListView = (SwipeMenuListView) view.findViewById(R.id.list_view);
        mButton = (Button) view.findViewById(R.id.button);
        mButton.setOnClickListener(this);
        mPreferenceUtils = new PreferenceUtils(getActivity());
        mPushSettingActivity = (PushSettingActivity) getActivity();
        mFindCarAdapter = new FindCarAdapter(mPushSettingActivity);
        mSwipeMenuListView.setNestedpParent(mPushSettingActivity.mViewPager);
        mSwipeMenuListView.setAdapter(mFindCarAdapter);
        mSwipeMenuListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                mHelpFindCarEntity = (RecordBean.DataEntity.HelpFindCarEntity) mFindCarAdapter.getItem(position);
                delete(mHelpFindCarEntity.getId());
            }
        });
        initSwipeMenu();
        return view;
    }



    private void initSwipeMenu() {
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                int width = getActivity().getWindowManager().getDefaultDisplay().getWidth();
                SwipeMenuItem item1 = new SwipeMenuItem(mPushSettingActivity);
                item1.setBackground(R.drawable.cancle_bg);
                item1.setTitle("取消\n提醒");
                item1.setTitleColor(getResources().getColor(R.color.white));
                item1.setTitleSize(15);
                item1.setWidth((width / 6));
                menu.addMenuItem(item1);
            }
        };
        mSwipeMenuListView.setMenuCreator(creator);
    }

    public void setData(RecordBean recordBean){
        mRecordBeans = recordBean.getData().getHelp_find_car();
        setNoRemind();
        mFindCarAdapter.setData(mRecordBeans);
    }

    @Override
    public void onClick(View v) {
        if(v == mButton){
            Intent intent = new Intent(mPushSettingActivity , MainActivity.class);
            intent.putExtra("type" , "findcarHistory");
            startActivity(intent);
        }
    }

    private void setNoRemind() {
        if (mRecordBeans.size() == 0) {
            view.findViewById(R.id.no_vip_find).setVisibility(View.VISIBLE);
        } else {
            view.findViewById(R.id.no_vip_find).setVisibility(View.GONE);
        }
    }


    public void delete(int id) {
        if (deleteNetDataJson == null) {
            deleteNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {
                @Override
                public void onDataJsonError(String errorMessage) {
                    mPushSettingActivity.loadingDialog.dismiss();
                    Toast.makeText(mPushSettingActivity, errorMessage, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onDataJsonUpdate(JSONObject json) {
                    mPushSettingActivity.loadingDialog.dismiss();
                    if (mHelpFindCarEntity != null) {
                        mRecordBeans.remove(mHelpFindCarEntity);
                        setNoRemind();
                        mFindCarAdapter.setData(mRecordBeans);
                        FindCarData mFindCarData = mPreferenceUtils.loadObject(FindCarData.class);
                        mFindCarData.clear();
                        mPreferenceUtils.saveObject(mFindCarData);
                        Toast.makeText(mPushSettingActivity, "删除成功", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
        deleteNetDataJson.setUrl(API.unsubscribe);
        deleteNetDataJson.addParam("type", "help_find_car");
        deleteNetDataJson.addParam("ids", id);
        deleteNetDataJson.request("post");
        mPushSettingActivity.loadingDialog.show();
    }
}
